<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\V3\V3_1\Models\CompanyClassModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * 企业类型
 * Class OverflowBreakageController
 * @package App\Http\Controllers\Api
 */
class CompanyClassController extends Controller
{
    /**
     * 列表
     * change 在 2021/10/21 11:27 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(){
        $model = CompanyClassModel::select('id','name','image','order')->orderBy('order','asc')->get();
        return success($model);
    }

    /**
     * 添加
     * change 在 2021/10/21 11:27 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' =>'required|string',//类型名称
            'image' =>'required|string',//图片地址
            'order' =>'required|integer',//排序
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['created_at'] = time();
        CompanyClassModel::insert($data);
        return success();
    }

    /**
     * 编辑
     * change 在 2021/10/21 11:27 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
            'name' =>'string',//类型名称
            'image' =>'string',//图片地址
            'order' =>'integer',//排序
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        CompanyClassModel::where('id',$data['id'])->update($data);
        return success();
    }

    /**
     *  删除
     * change 在 2021/10/21 11:27 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        CompanyClassModel::destroy($data['id']);
        return success();
    }
}
